package com.boot.security.server.utils.mffl.JPush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {

    /**
     * 发送极光推送 用户
     *
     * @param alias
     * @param title
     * @param msgContent
     * @return
     */
    public static int pushMsg(String alias, String title, String msgContent) {
        String MASTER_SECRET = CommonTools.getProperties("USER_SECRET");
        String APP_KEY = CommonTools.getProperties("USER_KEY");
        Thread thread = new Thread();
        thread.start();
        int result = 0;
        try {
            JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
            //PushPayload pushPayload = JPushTest.buildPushObject_all_registrationId_alertWithTitle(registrationId, notification_title, msg_title, msg_content, extrasparam);
            //别名推送
//            PushPayload pushPayload = JPushUtil.buildPushObject_android_and_iosByAlias(alias, title, msgContent);
            //RegistrationId 推送
            PushPayload pushPayload = JPushUtil.buildPushObject_android_and_iosByRegistrationId(alias,title,msgContent);
            System.out.println(pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            System.out.println(pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            //e.printStackTrace();
            System.out.println("用户不存在");
        } catch (APIRequestException e) {
           // e.printStackTrace();
            System.out.println("用户不存在");
        }
        return result;
    }

    /**
     * 发送极光推送 律师
     *
     * @param alias
     * @param title
     * @param msgContent
     * @return
     */
    public static int pushMsgLawyer(String alias, String title, String msgContent) {
        String MASTER_SECRET = CommonTools.getProperties("LAWYER_SECRET");
        String APP_KEY = CommonTools.getProperties("LAWYER_KEY");
        Thread thread = new Thread();
        thread.start();
        int result = 0;
        try {
            JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
            //PushPayload pushPayload = JPushTest.buildPushObject_all_registrationId_alertWithTitle(registrationId, notification_title, msg_title, msg_content, extrasparam);
            //别名推送
//            PushPayload pushPayload = JPushUtil.buildPushObject_android_and_iosByAlias(alias, title, msgContent);
            //RegistrationId 推送
            PushPayload pushPayload = JPushUtil.buildPushObject_android_and_iosByRegistrationId(alias,title,msgContent);
            System.out.println(pushPayload);
            PushResult pushResult = jPushClient.sendPush(pushPayload);
            System.out.println(pushResult);
            if (pushResult.getResponseCode() == 200) {
                result = 1;
            }
        } catch (APIConnectionException e) {
            //e.printStackTrace();
            System.out.println("用户不存在");
        } catch (APIRequestException e) {
            // e.printStackTrace();
            System.out.println("用户不存在");
        }
        return result;
    }

    /**
     * 别名推送
     *       
     */
    public static PushPayload buildPushObject_android_and_iosByAlias(String alias, String title, String content) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                //.setBigText(content)
                                .addExtra("type", "infomation")
                                .setStyle(1)
                                //.setBuilderId(1)
                                .setTitle(title)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("type", "infomation")
                                .setSound("default")
                                //.setBadge(+1)
                                .autoBadge()
                                //.incrBadge(+1)
                                //.setAlert(content)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)//true-推送生产环境 false-推送开发环境（测试使用参数）
                        .setTimeToLive(90)//消息在JPush服务器的失效时间（测试使用参数）
                        .build())
                .build();
    }


    /**
     * 别名推送
     *       
     */
    public static PushPayload buildPushObject_android_and_iosByRegistrationId(String registrationId, String title, String content) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                //.setBigText(content)
                                .addExtra("type", "infomation")
                                .setStyle(1)
                                //.setBuilderId(1)
                                .setTitle(title)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("type", "infomation")
                                .setSound("default")
                                //.setBadge(+1)
                                .autoBadge()
                                //.incrBadge(+1)
                                //.setAlert(content)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)//true-推送生产环境 false-推送开发环境（测试使用参数）
                        .setTimeToLive(90)//消息在JPush服务器的失效时间（测试使用参数）
                        .build())
                .build();
    }
}
